<template>
  <div class="drafts_wrapper">
    <div class="header">
      草稿箱<span>（{{ posts.length }}）</span>
    </div>
    <div v-if="posts.length">
      <div class="main" v-for="(post, index) in posts" :key="index">
        <div class="title">{{ post.title }}</div>
        <div class="info">
          <div class="time">{{ post.time }}</div>
          <div class="el-icon-edit iconfont" @click="handleWrite(index)"></div>
          <div
            class="el-icon-delete iconfont"
            @click="deleteDraft(index)"
          ></div>
        </div>
      </div>
    </div>
    <div class="draft_none" v-else>草稿箱空空如也~~</div>
  </div>
</template>

<script>
export default {
  computed: {
    posts() {
      return this.$store.state.post.posts;
    },
  },
  methods: {
    //   删除草稿箱
    deleteDraft(index) {
      this.$confirm("你确定要删除我吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$store.commit("post/removeDraft", index);
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    // 重写
    handleWrite(index) {
      this.$emit("reEdit", index);
    },
  },
};
</script>

<style lang="less" scoped>
.drafts_wrapper {
  width: 100%;
  border: 1px solid #ddd;
  box-sizing: border-box;
  .header {
    box-sizing: border-box;
    width: 100%;
    padding: 10px;
    font-size: 16px;
    color: #666;
    border-bottom: 1px solid #ddd;
  }
  .main {
    width: 100%;
    box-sizing: border-box;
    padding: 10px;
    font-size: 14px;
    .title {
      color: #000;
      /*单行文字溢出显示省略号*/
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .info {
      display: flex;
      align-items: center;
      .time {
        flex: 1;
        color: #999;
      }
      .iconfont {
        padding: 4px;
        cursor: pointer;
      }
    }
  }
  .draft_none {
    font-size: 14px;
    text-align: center;
    color: #555;
    box-sizing: border-box;
    padding: 20px 0;
  }
}
</style>